package com.ego.service;

import com.ego.spring.BeanPostProcessor;
import com.ego.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessorBeforeInitialization(String beanName, Object bean) {
        if(beanName.equals("userService")){
            System.out.println("postProcessorBeforeInitialization...");
        }
        return bean;
    }

    @Override
    public Object postProcessorAfterInitialization(String beanName, Object bean) {
        if(beanName.equals("userService")){
            System.out.println("postProcessorAfterInitialization...");
            return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("aop执行...");
                    return method.invoke(bean, args);
                }
            });
        }
        return bean;
    }
}
